package stackAndQueue;

import java.util.Stack;

public class N1047_RemoveDuplicates {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (stack.isEmpty() || stack.peek() != c)
                stack.push(c);
            else stack.pop();
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty())
            sb.append(stack.pop());

        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        N1047_RemoveDuplicates solution = new N1047_RemoveDuplicates();
        System.out.println(solution.removeDuplicates("abbaca"));
    }
}
